﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;


namespace syc.Store
{
    public partial class 产品入出库台账 : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.IsPostBack)
            {
                this.SqlDataSource3.SelectCommand = (string)this.ViewState["SelectCommand"];
            }
        }
        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }
        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {

            Label_Total.Text = "共查询到 " + e.AffectedRows + " 项记录";
        }
        private string getSql()
        {
            string sql = "select 原料供货单位,生产单位ID,入库序号,入库日期,炉号,支数,捆号,重量,长度,生产日期,库区,出库日期,运输方式,出库序号,[ID] from 产品入出库台账 where 入库序号 is not null";
            int chanpin = 0;
            int fenchang = 0;
            int gonghuo = 0;
            int dinghuo = 0;

            DateTime d1, d2, d3, d4;
            int.TryParse(this.ChanPin.Text, out chanpin);
            int.TryParse(this.FenChang.Text, out fenchang);
            int.TryParse(this.GongHuo.Text, out gonghuo);
            int.TryParse(this.DingHuo.Text, out dinghuo);
            if (chanpin != 0)
            {
                sql += " and 产品类别ID=" + chanpin;
            }
            if (fenchang != 0)
            {
                sql += " and 生产单位ID=" + fenchang;
            }
            if (gonghuo != 0)
            {
                sql += " and 原料供货ID=" + gonghuo;

            }
            if (dinghuo != 0)
            {
                sql += " and 订货单位ID=" + dinghuo;

            }

            if (DateTime.TryParse(Date1.Text, out d1))
            {
                sql += " and 入库日期>='" + d1.ToShortDateString() + "'";
            }
            else if (Date1.Text != "")
            {
                this.Label_Result.Text = "入库起始日期输入有误";
                return "";
            }

            if (DateTime.TryParse(Date2.Text, out d2))
            {
                sql += " and 入库日期<='" + d2.ToShortDateString() + "'";
            }
            else if (Date2.Text != "")
            {
                this.Label_Result.Text = "入库截止日期输入有误";
                return "";
            }

            if (DateTime.TryParse(Date3.Text, out d3))
            {
                sql += " and 出库日期>='" + d3.ToShortDateString() + "'";
            }
            else if (Date3.Text != "")
            {
                this.Label_Result.Text = "出库起始日期输入有误";
                return "";
            }
            if (DateTime.TryParse(Date4.Text, out d4))
            {
                sql += " and 出库日期<='" + d4.ToShortDateString() + "'";
            }
            else if (Date4.Text != "")
            {
                this.Label_Result.Text = "出库截止日期输入有误";
                return "";
            }
            return sql;
        }
        protected void Button_Query_Click(object sender, EventArgs e)
        {
            this.SqlDataSource3.SelectCommand = getSql();
            this.ViewState["SelectCommand"] = getSql();
        }
        protected void Button_Export_Click(object sender, EventArgs e)
        {
            string sql = getSql();
            if (sql == "") return;

            Response.Charset = "utf-8";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.ContentType = "application/ms-word";
            Response.AddHeader("content-disposition", "attachment; filename=\"" + Server.UrlEncode("产品入出库台账.doc") + "\"");
            Response.Write(
    @"<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">
<html>
<head>
<meta http-equiv=""Content-Language"" content=""zh-cn""/>
<meta http-equiv=""Content-Type"" content="""
                + Response.ContentType + "; charset=utf-8\"/></head><body>");

            Response.Write(
@"<style>
@page Section1{size:23.0cm 16.0cm;margin:1.5cm 0.5cm 0.5cm 0.5cm;size:29.7cm 21.0cm;text-align:center;}
div.Section1{page:Section1;}
td.c0{height:50px;border:none;font-size:20pt;font-weight:bold;text-align:center;line-height:50px;}
td.c1{width:120px;height:25px;border:1px solid #000000;}
td.c2{width:36px;border:1px solid #000000;}
td.c3{width:80px;border:1px solid #000000;}
td.c4{width:90px;border:1px solid #000000;}
td.c5{width:70px;border:1px solid #000000;}
td.c6{height:32px;border:1px solid #000000;}
</style><div class=Section1>");

            string head =
@"<table border=0 cellpadding=0 cellspacing=0 style=text-align:center;border-collapse:collapse;font-size:11pt;>
	<tr>
		<td class=c0 colspan=13>成品入出库登记台帐</td>
	</tr>
	<tr height=25>
		<td colspan=2 align=left style=padding-left:20px;>　产品名称：" + this.ChanPin.SelectedItem.Text + @"</td>
		<td colspan=2>钢级：J55</td>
		<td colspan=3 align=left style=padding-left:8px;>规格：</td>
		<td colspan=4 align=left style=padding-left:8px;>订货单位：</td>
		<td colspan=2>编码：SYC-ZJ-WL-11</td>
	</tr>
	<tr height=38>
		<td class=c1>原料供货单位</td>
		<td class=c3>入库序号</td>
		<td class=c3>入库日期</td>
		<td class=c3>炉号</td>
		<td class=c2>支数</td>
		<td class=c4>捆编号</td>
		<td class=c5>捆重量(kg)</td>
		<td class=c5>捆长度(m)</td>
		<td class=c3>生产日期</td>
		<td class=c2>存放区域</td>
		<td class=c3>出库日期</td>
		<td class=c3>发运方式</td>
		<td class=c3>出库序号</td>
	</tr>";
            string foot =
@"<tr height=25>
		<td colspan=3 align=left style=padding-left:30px;>仓储主管：</td>
		<td colspan=7></td>
		<td colspan=3 align=left style=padding-left:30px;>保管员：</td>
	</tr>
</table>";
            Response.Write(head);
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            Cmd.CommandText = sql;
            Cmd.CommandType = CommandType.Text;

            SqlDataReader Reader = Cmd.ExecuteReader();
            int line = 0;
            int zhi = 0;//累计支数
            int kun = 0;//累计捆数
            double zhong = 0;//累计捆重
            double chang = 0;//累计捆长
            string xuhao0 = "";
            string xuhao = "";
            while (Reader.Read())
            {

                if (!Reader.IsDBNull(2))
                {
                    xuhao = (byte)Reader[1] + "-" + (string)Reader[2];
                }
                if (xuhao != xuhao0 && kun > 0)
                {
                    Response.Write("<tr><td class=c6 colspan=13>合计： " + zhi + "支&nbsp;&nbsp;&nbsp;&nbsp;" + kun + "捆&nbsp;&nbsp;&nbsp;&nbsp;" + (zhong / 1000) + "吨&nbsp;&nbsp;&nbsp;&nbsp;" + chang + "m</td></tr>");
                    line++;
                    zhi = 0;
                    kun = 0;
                    zhong = 0;
                    chang = 0;
                }
                xuhao0 = xuhao;
                zhi += (byte)Reader["支数"];
                kun++;
                zhong += (double)Reader["重量"];
                chang += (double)Reader["长度"];
                Response.Write("<tr><td class=c1>");
                if (!Reader.IsDBNull(0))
                {
                    Response.Write((string)Reader[0]);
                }
                Response.Write("</td><td class=c2>");
                if (!Reader.IsDBNull(2))
                {
                    Response.Write(xuhao);
                }

                Response.Write("</td><td class=c3>" + ((DateTime)Reader["入库日期"]).ToString("yyyMMdd") + "</td>"
                    + "<td class=c3>" + (string)Reader["炉号"] + "</td>"
                    + "<td class=c2>" + (byte)Reader["支数"] + "</td>"
                    + "<td class=c4>" + (string)Reader["捆号"] + "</td>"
                    + "<td class=c5>" + (double)Reader["重量"] + "</td>"
                    + "<td class=c5>" + (double)Reader["长度"] + "</td>"
                    + "<td class=c3>" + ((DateTime)Reader["生产日期"]).ToString("yyyMMdd") + "</td>"
                    + "<td class=c2>" + (byte)Reader["库区"] + "</td><td class=c3>"
                    );
                if (!Reader.IsDBNull(11))
                {
                    Response.Write(((DateTime)Reader[11]).ToString("yyyMMdd"));
                }
                Response.Write("</td><td class=c3>");
                if (!Reader.IsDBNull(12))
                {
                    Response.Write((string)Reader[12]);
                }
                Response.Write("</td><td class=c3>");
                if (!Reader.IsDBNull(13))
                {
                    Response.Write((string)Reader[13]);
                }
                Response.Write("</td></tr>");
                line++;


                if (line >= 20)
                {
                    Response.Write(foot);
                    Response.Write("<br clear=all style='mso-special-character:line-break;page-break-before:always'>");
                    Response.Write(head);
                    line = 0;
                }
            }
            if (kun > 0)
            {
                Response.Write("<tr><td class=c6 colspan=13>合计： " + zhi + "支&nbsp;&nbsp;&nbsp;&nbsp;" + kun + "捆&nbsp;&nbsp;&nbsp;&nbsp;" + (zhong / 1000) + "吨&nbsp;&nbsp;&nbsp;&nbsp;" + chang + "m</td></tr>");
                line++;
            }


            Reader.Close();
            Reader.Dispose();
            Cmd.Dispose();
            Conn.Close();
            Conn.Dispose();

            if (line > 0)
            {
                for (int i = line; i < 20; i++)
                {
                    Response.Write(
 @"<tr>
<td class=c1></td>
<td class=c2></td>
<td class=c3></td>
<td class=c3></td>
<td class=c2></td>
<td class=c4></td>
<td class=c5></td>
<td class=c5></td>
<td class=c3></td>
<td class=c2></td>
<td class=c3></td>
<td class=c3></td>
<td class=c3></td>
</tr>");
                }
                Response.Write(foot);
            }
            Response.Write("</div></body></html></body></html>");
            Response.End();

        }



    }
}

